#ifndef ODN_INPUTBINDINGMGR_H
#define ODN_INPUTBINDINGMGR_H

#include "ODN_InputDevice.h"
#include "ODN_Events.h"
#include "ODN_LogMgr.h"

namespace ouden {

class CInputBindingMgr : public IInputReceiver
{
	public:
		CInputBindingMgr();
		virtual ~CInputBindingMgr();
		
		void bindKeyDownToEvent(KeyCode iKey, IEvent* pEvent)
		{
			
			//Get easy reference to bound event
			IEvent* &pBoundEvent = m_pKeyEventBindings[iKey].pDownEvent;
			
			//Check if the key is already bound to an event
			if (pBoundEvent) { 
				//TODO: Decide if this log message is needed
				ODN_LOG(LML_DEBUG) << "Key " << iKey << " already bound, overwriting binding.";
				
				delete pBoundEvent;
			}
			
			pBoundEvent = pEvent;
			
		}
		
		void bindKeyUpToEvent(KeyCode iKey, IEvent* pEvent)
		{
			
			//Get easy reference to bound event
			IEvent* &pBoundEvent = m_pKeyEventBindings[iKey].pUpEvent;
			
			//Check if the key is already bound to an event
			if (pBoundEvent) { 
				//TODO: Decide if this log message is needed
				ODN_LOG(LML_DEBUG) << "Key " << iKey << " already bound, overwriting binding.";
				
				delete pBoundEvent;
			}
			
			pBoundEvent = pEvent;
			
		}
		
		void bindKeyPressToEvent(KeyCode iKey, IEvent* pEvent)
		{
			
			//Get easy reference to bound event
			IEvent* &pBoundEvent = m_pKeyEventBindings[iKey].pPressEvent;
			
			//Check if the key is already bound to an event
			if (pBoundEvent) { 
				//TODO: Decide if this log message is needed
				ODN_LOG(LML_DEBUG) << "Key " << iKey << " already bound, overwriting binding.";
				
				delete pBoundEvent;
			}
			
			pBoundEvent = pEvent;
			
		}
		
		virtual void onUnBufInput(IInputDevice* pInput) 		{}
		virtual void onKeyDown(KeyCode iKey)
		{
			IEvent* pEvent = m_pKeyEventBindings[iKey].pDownEvent;
			if (pEvent)
				pEvent->activate();
		}
		virtual void onKeyUp(KeyCode iKey)
		{
			IEvent* pEvent = m_pKeyEventBindings[iKey].pUpEvent;
			if (pEvent)
				pEvent->activate();
		}
		virtual void onKeyPress(KeyCode iKey)
		{
			IEvent* pEvent = m_pKeyEventBindings[iKey].pPressEvent;
			if (pEvent)
				pEvent->activate();
		}
		//TODO: Decide if need bindings for these mouse related events
		virtual void onMouseMove(Vector vPos, MouseButton iButtonState) 		{}
		virtual void onMouseButtonUp(Vector vPos, MouseButton iButton) 			{}
		virtual void onMouseButtonDown(Vector vPos, MouseButton iButton) 		{}
		virtual void onMouseButtonPress(Vector vPos, MouseButton iButton) 		{}
		
	protected:
	private:
		
		//Struct for a key binding, which includes pointers to events bound to
		// key up, down and press events
		struct SKeyBinding {
			IEvent* pDownEvent;
			IEvent* pUpEvent;
			IEvent* pPressEvent;
		};
		
		SKeyBinding m_pKeyEventBindings[256]; 		//Array of key bindings
		
};

} //namespace ouden

#endif // ODN_INPUTBINDINGMANAGER_H
